package com.hiray.demos.rv_related

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import android.widget.Toast
import com.hiray.demos.ItemAdapter
import com.hiray.demos.ItemAdapter.OnItemClickListener
import com.hiray.demos.R
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration
import kotlinx.android.synthetic.main.activity_grid_layout_divider.*

class GridLayoutDividerActivity : AppCompatActivity() {

    val items = arrayListOf<String>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_grid_layout_divider)
        loadData()
        recyclerview.adapter = ItemAdapter(items, object : OnItemClickListener {
            override fun onItemClick(position: Int) {
                //no op
            }

        })
        val gridLayoutManager = androidx.recyclerview.widget.GridLayoutManager(this, 3)

        gridLayoutManager.spanSizeLookup = object : androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                if (position == 0 || position == 5 || position == 14)
                    return 2
                return 1
            }

        }
        recyclerview.layoutManager = gridLayoutManager
        recyclerview.addItemDecoration(HorizontalDividerItemDecoration.Builder(this@GridLayoutDividerActivity)
                .colorResId(R.color.colorAccent)
                .size(3)
                .build())


        button.setOnClickListener {
            val pos = position_editor.text.toString().toInt()
            val spanIndex = gridLayoutManager.spanSizeLookup.getSpanIndex(pos, 3)
            Toast.makeText(this@GridLayoutDividerActivity, spanIndex.toString(), Toast.LENGTH_SHORT).show()
        }
    }

    private fun loadData() {
        for (i in 0..25)
            items.add(i.toString())
    }
}
